package com.zrj.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrj.auth.mapper.UmsMemberMapper;
import com.zrj.auth.pojo.dto.MemberAuthInfoDTO;
import com.zrj.auth.pojo.dto.MemberDTO;
import com.zrj.auth.pojo.entity.UmsMember;
import com.zrj.auth.service.IUmsMemberService;
import org.springframework.stereotype.Service;

/**
 * @author zhaorujie
 */
@Service
public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberMapper, UmsMember> implements IUmsMemberService {
    @Override
    public MemberAuthInfoDTO getByOpenid(String openid) {
        LambdaQueryWrapper<UmsMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMember::getOpenid, openid)
                .select(UmsMember::getId,
                        UmsMember::getOpenid,
                        UmsMember::getStatus);
        UmsMember member = this.getOne(wrapper);
        MemberAuthInfoDTO memberAuth = null;
        if (member != null) {
            memberAuth = new MemberAuthInfoDTO()
                    .setMemberId(member.getId())
                    .setUsername(member.getOpenid())
                    .setStatus(member.getStatus());
        }
        return memberAuth;
    }

    @Override
    public MemberAuthInfoDTO getByMobile(String mobile) {
        UmsMember member = this.getOne(new LambdaQueryWrapper<UmsMember>()
                .eq(UmsMember::getMobile, mobile)
                .select(UmsMember::getId,
                        UmsMember::getMobile,
                        UmsMember::getStatus));
        MemberAuthInfoDTO memberAuth = null;
        if (member != null) {
            memberAuth = new MemberAuthInfoDTO()
                    .setMemberId(member.getId())
                    .setUsername(member.getMobile())
                    .setStatus(member.getStatus());
        }
        return memberAuth;
    }

    @Override
    public void addMember(MemberDTO memberDTO) {
        UmsMember umsMember = BeanUtil.copyProperties(memberDTO, UmsMember.class);
        this.save(umsMember);
    }
}
